package org.infodavid.common.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;

import org.infodavid.common.dto.IDocumentCategoryListItem;
import org.infodavid.common.impl.model.criteria.CategoryCriteriaImpl;
import org.infodavid.common.model.DocumentCategoryTest;
import org.infodavid.common.model.IDocumentCategory;
import org.infodavid.common.model.criteria.ICategoryCriteria;

/**
 * The Class DocumentCategoryDataServiceTest.
 */
@SuppressWarnings({
	"null"
})
public class DocumentCategoryDataServiceTest extends
    AbstractDataServiceTest<IDocumentCategoryListItem,IDocumentCategory,Long> {

	/**
	 * The Constructor.
	 */
	public DocumentCategoryDataServiceTest() {
		super(new DocumentCategoryTest(), DocumentCategoryDataService.getInstance());
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.AbstractDataServiceTest#modify(org.infodavid.common.model.IDataObject
	 * )
	 */
	@Override
	public void modify(final IDocumentCategory item) {
		item.setComment("ModifiedComment");
	}

	/**
	 * Test get instance.
	 */
	@Test
	public final void testGetInstance() {
		Assert.assertEquals("Not a singleton", DocumentCategoryDataService.getInstance(),
		    DocumentCategoryDataService.getInstance());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteria() throws Exception {
		final List<IDocumentCategoryListItem> results = new ArrayList<IDocumentCategoryListItem>();
		final ICategoryCriteria criteria = new CategoryCriteriaImpl();
		final long count =
		    ((DocumentCategoryDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingAllCriterionsl() throws Exception {
		final Collection<IDocumentCategory> inserted = getTestData();
		final List<IDocumentCategoryListItem> results = new ArrayList<IDocumentCategoryListItem>();
		final ICategoryCriteria criteria = new CategoryCriteriaImpl();

		criteria.setNamePattern(inserted.iterator().next().getName());

		final long count =
		    ((DocumentCategoryDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", 1, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNameNoDataFound() throws Exception {
		final List<IDocumentCategoryListItem> results = new ArrayList<IDocumentCategoryListItem>();
		final ICategoryCriteria criteria = new CategoryCriteriaImpl();

		criteria.setNamePattern("NoData");

		final long count =
		    ((DocumentCategoryDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 0, results.size());
		Assert.assertEquals("Not all data has been found", 0, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingEqual() throws Exception {
		final Collection<IDocumentCategory> inserted = getTestData();
		final List<IDocumentCategoryListItem> results = new ArrayList<IDocumentCategoryListItem>();
		final ICategoryCriteria criteria = new CategoryCriteriaImpl();

		criteria.setNamePattern(inserted.iterator().next().getName());

		final long count =
		    ((DocumentCategoryDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", 1, results.size());
		Assert.assertEquals("Not all data has been found", 1, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNameLike() throws Exception {
		final List<IDocumentCategoryListItem> results = new ArrayList<IDocumentCategoryListItem>();
		final ICategoryCriteria criteria = new CategoryCriteriaImpl();

		criteria.setNamePattern("item%");

		final long count =
		    ((DocumentCategoryDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCollection() throws Exception {
		final ICategoryCriteria criteria = new CategoryCriteriaImpl();
		Exception exception = null;

		try {
			((DocumentCategoryDataService)getService()).find(getSession(), null, criteria);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNotNull("No exception thrown", exception);
		Assert.assertEquals("Wrong exception thrown", IllegalArgumentException.class,
		    exception.getClass());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingNullCriteria() throws Exception {
		final List<IDocumentCategoryListItem> results = new ArrayList<IDocumentCategoryListItem>();
		final long count =
		    ((DocumentCategoryDataService)getService()).find(getSession(), results,
		        (ICategoryCriteria)null);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByCriteriaUsingPage() throws Exception {
		final List<IDocumentCategoryListItem> results = new ArrayList<IDocumentCategoryListItem>();
		final ICategoryCriteria criteria = new CategoryCriteriaImpl();

		criteria.setPageStart(Long.valueOf(0));
		criteria.setPageSize(Integer.valueOf(5));

		final long count =
		    ((DocumentCategoryDataService)getService()).find(getSession(), results, criteria);

		//System.out.println(results);

		Assert.assertNotNull("Result is null", results);
		Assert.assertEquals("Not all data has been found", criteria.getPageSize().intValue(),
		    results.size());
		Assert.assertEquals("Not all data has been found", ITEMS_TO_INSERT, count);
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByName() throws Exception {
		final Collection<IDocumentCategory> inserted = getTestData();
		final IDocumentCategory result =
		    ((DocumentCategoryDataService)getService()).findByName(getSession(), inserted.iterator()
		        .next().getName());

		Assert.assertNotNull("Result is null", result);
		Assert.assertEquals("Wrong data found", inserted.iterator().next().getName(), result.getName());
		Assert.assertEquals("Wrong data found", inserted.iterator().next().getKey(), result.getKey());
	}

	/**
	 * Test find.
	 * @throws Exception the exception
	 */
	@Test
	public void testFindByNameUsingNull() throws Exception {
		Exception exception = null;

		try {
			((DocumentCategoryDataService)getService()).findByName(getSession(), null);
		}
		catch (final Exception e) {
			exception = e;
			e.printStackTrace();
		}

		Assert.assertNull("Exception thrown", exception);
	}
}
